<?php

class SF_Customersource_TransformController extends Mage_Core_Controller_Front_Action 
{ 

	public function indexAction()
	{
		$this->addFlagToSessionAction();
	}
	
	protected function addFlagToSessionAction() 
    { 
    	$product_id = trim($this->getRequest()->getParam('sku'));
		
		if(!empty($product_id))
		{
			//判断productid是否是组合产品的子产品，还是单品。如果是子产品，需要获取组合产品的url。
			$configurable_ids  = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($product_id);
			
			if(count($configurable_ids) > 0)
			{
				$this->product_id = $configurable_ids[0];
			}
			else
			{
				$this->product_id = $product_id;
			}
			$this->redirectAction();
		}
		else
		{
			$this->_redirectUrl(Mage::getBaseUrl());    //跳转到首页
		}
    }
	
	protected function redirectAction() 
    { 
		$_product       = Mage::getModel("catalog/product")->load($this->product_id);
		$_category      = Mage::getModel('catalog/category');
		
		if(is_object($_product))
		{
			$urlKey = $_product->getUrlKey();
			$this->_redirectUrl(Mage::getBaseUrl() . $urlKey);
		}
		else
		{
			$this->_redirectUrl(Mage::getBaseUrl());    //跳转到首页
		}

    }
    
    public function getSessionAction()
    {
    /*	var_dump($_SESSION);*/
/* var_dump(Mage::helper('passport')->getPassport('6703')); */
    }
    
    public function createCustonOptionsAction()
    {
		Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
		 
		$productId = '995';
		$product = Mage::getModel('catalog/product')->load($productId);
		 
		$options = array(
						'title' => 'Color',
						'type' => 'drop_down',
						'is_required' => 1,
						'sort_order' => 0,
						'values' => array(
										array(
											'title' => 'Red',
											'price' => 10.50,
											'price_type' => 'fixed',
											'sku' => '',
											'sort_order' => 0,
										),
										array(
											'title' => 'Gold',
											'price' => 0,
											'price_type' => 'percent',
											'sku' => 'test-product-sku',
											'sort_order' => 0,
										)
									)
					);
		 
		$product->setProductOptions(array($options));
		$product->setCanSaveCustomOptions(true);
		$product->save();
    }

    public function removeCustonOptionsAction()
    {
		$productId = '995';
		$product = Mage::getModel('catalog/product')->load($productId);
		 
		$customOptions = $product->getOptions();
		 
		foreach ($customOptions as $option) {
			$option->delete();
		}
		 
		$product->setHasOptions(0);
		$product->setUrlKey(false);
		$product->save();
    }

    public function testOrderAction()
    {
    	// $order = Mage::getModel('sales/order')->loadByIncrementId('100001127');
    	// echo $order->getCreatedAt();
    	// echo '<hr>';
    	// echo "<pre>";
    	// print_r($order->getData());
    	// echo '</pre>';

		// Mage::helper('saleorderservice')->pushOrderInfo('100001127');
		// $increment_id = '100000100';
		// $subOrder = Mage::getModel('saleorderservice/order')->getCollection();
		// $subOrder->getSelect()->where("increment_id like '%{$increment_id}%'");
		// echo $subOrder->getSelect();
  //       var_dump($subOrder->getData());
  //       foreach ($subOrder->getData() as $key => $value) {
  //       	var_dump($value['increment_id']);
  //       }
        
    				/* 寄件方信息 */
			$VendorId = '17';
			$rs 	  = Mage::getSingleton('core/resource')->getConnection('core_read')
					    ->fetchAll("select vendor_id,email from ves_vendor where entity_id = ". $VendorId);
			$rs1 	  = Mage::getSingleton('core/resource')->getConnection('core_read')
			 			->fetchAll("SELECT attribute_id,value,entity_id FROM ves_vendor_varchar WHERE entity_id = "
			 			. $VendorId);

			$CompanyCode 		= $rs[0]['vendor_id'];	/*货主编码*/
			$SenderEmail 		= $rs[0]['email']; /*寄件方电子邮箱*/
			$SenderArea 		= ''; /*寄件方区县*/


			foreach ($rs1 as $key => $value)
			{
				switch ($value['attribute_id'])
				 {
					case '182':
						$SenderCompany 	= $value['value']; /*寄件方公司*/
						break;
					case '181':
						$SenderName 	= $value['value']; /*寄件方名称*/
						break;
					case '190':
						$SenderZipCode 	= $value['value']; /*寄件方邮编*/
						break;
					case '191':
						$SenderMobile 	= $value['value']; /*寄件方手机*/
						$SenderPhone 	= $value['value']; /*寄件方电话*/
						break;
					case '187':
						$SenderProvince = $value['value']; /*寄件方省份*/
						break;
					case '186':
						$SenderCity 	= $value['value']; /*寄件方城市*/
						break;
					case '185':
						$SenderAddress 	= $value['value']; /*寄件方地址*/
						break;
					case '189':
						$SenderCountry  = $value['value']; /*寄件方国家*/
						break;
				}
			}

			echo $CompanyCode.'<br>';
			echo $SenderEmail.'<br>';
			echo $SenderArea.'<br>';
			echo $SenderCompany.'<br>';
			echo $SenderName.'<br>';
			echo $SenderZipCode.'<br>';
			echo $SenderMobile.'<br>';
			echo $SenderPhone.'<br>';
			echo $SenderProvince.'<br>';
			echo $SenderCity.'<br>';
			echo $SenderAddress.'<br>';
			echo $SenderCountry.'<br>';
    }
}
